import cv2
import numpy as np
img = cv2.imread(r"/\images\005.jpeg", cv2.IMREAD_GRAYSCALE)

height = img.shape[0]
width = img.shape[1]
dsize = (width, height)

ret, img1 = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY)
# cv2.RETR_TREE：轮廓的检索模式
# cv2.CHAIN_APPROX_SIMPLE：轮廓的近似方法
# contours：轮廓 list[array]
contours, _ = cv2.findContours(img1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img2 = np.zeros(img.shape, np.uint8) + 255
img2 = cv2.drawContours(img2, contours, -1, (0, 0, 255))

pic = [("img", img), ("img1", img1), ("img2", img2)]
for name, val in pic:
    cv2.imshow(name, val)
    key = cv2.waitKey(5000)
    if key == 27:
        break
cv2.destroyAllWindows()
